package com.kvest.maptest.ui.activity;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.*;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.ImageButton;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.kvest.maptest.R;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: roman
 * Date: 9/4/13
 * Time: 9:07 AM
 * To change this template use File | Settings | File Templates.
 */
public class MapGroundOverlayActivity extends FragmentActivity {
    private GroundOverlayOptions overlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_map);

        init();
    }

    private void init() {
        final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(46.444200,30.756184), 15.0f));
        overlay = createGroundOverlayOptions();
        map.addGroundOverlay(overlay);
    }

    private GroundOverlayOptions createGroundOverlayOptions() {
        GroundOverlayOptions groundOverlayOptions  = new GroundOverlayOptions();
        //groundOverlayOptions.image(BitmapDescriptorFactory.fromBitmap(getBitmap("pobeda.png")));
        groundOverlayOptions.image(BitmapDescriptorFactory.fromBitmap(getBitmap("pobeda_full.png")));
        groundOverlayOptions.position(new LatLng(46.448300/*54*/, 30.747852/*815*/), 690, 690);
        groundOverlayOptions.anchor(0.0f, 0.0f);
        groundOverlayOptions.visible(true);
        groundOverlayOptions.transparency(0.0f);

        return groundOverlayOptions;
    }

    private Bitmap getBitmap(String filename) {
        Bitmap bitmap = getBitmapFromAsset(this, filename);
        return prepareBitmap(bitmap);
    }

    private Bitmap prepareBitmap(Bitmap src) {
        //Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
        Log.d("KVEST_TAG", src.getHeight() + "->" + nextPOT(src.getHeight()));
        Log.d("KVEST_TAG", src.getWidth() + "->" + nextPOT(src.getWidth()));
        Bitmap bmp = Bitmap.createBitmap(256, 256, src.getConfig()); // this creates a MUTABLE bitmap
        Canvas canvas = new Canvas(bmp);
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(src, new Rect(0, 0, 256, 256), new Rect(0, 0, 256, 256), null);
       // canvas.drawBitmap(src, 0, 0, null);
        return bmp;//Bitmap.createBitmap(src, 0, 0, 256, 256);
    }

    public static Bitmap getBitmapFromAsset(Context context, String fileName) {
        AssetManager assetManager = context.getAssets();

        InputStream is;
        Bitmap bitmap = null;
        try {
            is = assetManager.open(fileName);
            bitmap = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            return null;
        }

        return bitmap;
    }

    private static int nextPOT(int x)
    {
        x = x - 1;
        x = x | (x >> 1);
        x = x | (x >> 2);
        x = x | (x >> 4);
        x = x | (x >> 8);
        x = x | (x >>16);
        x = x | (x >>32);
        return x + 1;
    }
}